package pl.edu.agh.anomalyids.launcher;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pl.edu.agh.anomalyids.launcher.util.LauncherUtils;
import pl.edu.agh.anomalyids.lerad.listener.PacketVerifier;
import pl.edu.agh.anomalyids.lerad.rule.RulesHolder;
import pl.edu.agh.anomalyids.lerad.rule.util.RuleReader;
import pl.edu.agh.anomalyids.network.Interface;
import pl.edu.agh.anomalyids.network.sniffer.Sniffer;

public class WatchLauncher {

	private List<Interface> interfaces;
	private Logger logger = LoggerFactory.getLogger(WatchLauncher.class);

	public static void main(String[] args) {
		if (args.length < 1) {
			System.out.println("Correct arguments: <input filet path with rules>");
			System.exit(-1);
		}

		WatchLauncher launcher = new WatchLauncher();

		RulesHolder rulesHolder = new RulesHolder();
		rulesHolder.addRules(new RuleReader().read(args[0]));

		launcher.interfaces = Sniffer.getInterfacesList();
		LauncherUtils.printInterfaces(launcher.interfaces);
		String input = LauncherUtils.getUserInput(launcher.logger);

		try {
			String interfaceName = launcher.interfaces.get(
					Integer.valueOf(input)).getName();

			Sniffer sniffer = new Sniffer(interfaceName);

			PacketVerifier verifier = new PacketVerifier(rulesHolder);
			sniffer.addPacketListener(verifier);

			sniffer.start();
			sniffer.join();
		} catch (InterruptedException e) {
			launcher.logger.error("Sniffer interrupted.", e);
		}
	}

}
